// Copyright (c) 2025, 大连理工大学 (Dalian University of Technology)
//
// Licensed under the Mulan PSL v2.
// You can use this file according to the terms and conditions of the Mulan PSL v2.
// You may obtain a copy of the License at
//
//     http://license.coscl.org.cn/MulanPSL2
//
// THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
// EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
// MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. SEE THE MULAN PSL v2
// FOR MORE DETAILS.

/*
 * @Author: qiulin_JXGM ql1746365080@gmail.com
 * @Date: 2024-10-11 15:36:29
 * @LastEditors: qiulin_JXGM ql1746365080@gmail.com
 * @LastEditTime: 2024-10-15 16:39:18
 * @FilePath: \SGFEM\Import\BDFImport\BulkData\Coordinate\CORD2CParser.cpp
 */

#include "Import/BDFImport/BulkData/Coordinate/CORD2CParser.h"

#include <algorithm>
#include <cstdint>
#include <iostream>

#include "Utility/Logging/SGLogger.h"

#include "DBManager/DBServiceFactory.h"
#include "DBManager/IService/Input/Coordinate/ICORD2CService.h"

#include "Import/BDFImport/BDFParserBase.h"

BULKDATA_ENTRY_PARSER_REG (CORD2C);

using namespace SG::DBManager;
using namespace SG::Import;
using namespace SG::DataStructure;

/*
// file://./../../Doc/MarkDown/BulkData/CORD2C/CORD2C.md>
*/
// 以下需要修改
enum CORD2CKey : std::uint8_t
{
    CORD2CKEY_CID = 2,
    CORD2CKEY_RID = 3,  // Identification number of a coordinate system that is defined independently from this coordinate system. (Integer
                        // ≥ 0; Default = 0 is the basic coordinate system.)
    CORD2CKEY_A1 = 4,   // Coordinates of three points given with respect to the coordinate system defined byRID. (Real)
    CORD2CKEY_A2 = 5,
    CORD2CKEY_A3 = 6,
    CORD2CKEY_B1 = 7,
    CORD2CKEY_B2 = 8,
    CORD2CKEY_B3 = 9,
    CORD2CKEY_C1 = 12,
    CORD2CKEY_C2 = 13,
    CORD2CKEY_C3 = 14,
};
SG::DataStructure::Common::Status BDF::BULKDATA::ParseCORD2C (const std::shared_ptr<SG::DBManager::DBServiceFactory>& dbServiceFactory,
                                                              std::vector<std::string>&                               dividedEntryInfo)
{
    // 创建一个临时的 CORD2CData 对象，用于存储解析出的坐标系数据
    SG::DataStructure::FEM::CORD2CData tempCORD2C;

    // TODO(bug 001) m_dominId的正确填写
    tempCORD2C.m_dominId = 1;

    // 初始化当前字段的索引值
    int curIDKey = 1;

    // 遍历分割后的词条信息
    for (auto curStrKey : dividedEntryInfo)
    {
        // 检查当前字段是否为空白，如果为空则跳过
        bool const isEmpty = is_all_space (curStrKey);
        if (isEmpty)
        {
            curIDKey++;  // 增加索引以便继续遍历
            continue;
        }

        // 根据索引值判断当前字段的含义，并解析字段内容
        switch (curIDKey)
        {
        case CORD2CKEY_CID:
            // 解析坐标系的识别号
            tempCORD2C.m_id = convertToI64 (curStrKey);
            break;
        case CORD2CKEY_RID:
            // 解析参考坐标系的识别号
            tempCORD2C.m_rId = convertToI64 (curStrKey);
            break;
        case CORD2CKEY_A1:
            // 解析第一个点 A 的第一个坐标
            tempCORD2C.m_a1 = convertToReal (curStrKey);
            break;
        case CORD2CKEY_A2:
            // 解析第一个点 A 的第二个坐标
            tempCORD2C.m_a2 = convertToReal (curStrKey);
            break;
        case CORD2CKEY_A3:
            // 解析第一个点 A 的第三个坐标
            tempCORD2C.m_a3 = convertToReal (curStrKey);
            break;
        case CORD2CKEY_B1:
            // 解析第二个点 B 的第一个坐标
            tempCORD2C.m_b1 = convertToReal (curStrKey);
            break;
        case CORD2CKEY_B2:
            // 解析第二个点 B 的第二个坐标
            tempCORD2C.m_b2 = convertToReal (curStrKey);
            break;
        case CORD2CKEY_B3:
            // 解析第二个点 B 的第三个坐标
            tempCORD2C.m_b3 = convertToReal (curStrKey);
            break;
        case CORD2CKEY_C1:
            // 解析第三个点 C 的第一个坐标
            tempCORD2C.m_c1 = convertToReal (curStrKey);
            break;
        case CORD2CKEY_C2:
            // 解析第三个点 C 的第二个坐标
            tempCORD2C.m_c2 = convertToReal (curStrKey);
            break;
        case CORD2CKEY_C3:
            // 解析第三个点 C 的第三个坐标
            tempCORD2C.m_c3 = convertToReal (curStrKey);
            break;
        default:
            // 处理其他无关字段
            break;
        }
        // 增加当前字段的索引以便处理下一个字段
        curIDKey++;
    }

    // 将解析后的 CORD2C 数据添加到数据库
    return dbServiceFactory->get<ICORD2CService> ()->Add (tempCORD2C);
}